package com.windoent.yugioh.widget;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

/**
 * author : robert
 * date   : 2021/5/8 10:41
 * desc   :
 */
public class HexagonImageView extends Drawable {

    Rect mRect = new Rect();
    Paint mPaint;
    Path mPath;
    BitmapShader mShader;
    Bitmap mBitmap;
    private Matrix mShaderMatrix = new Matrix();

    public HexagonImageView() {
        this(null, null, null);
    }

    public HexagonImageView(Bitmap bitmap, Integer width, Integer height) {
        init();

        setBitmap(bitmap, width, height);
    }


    private void init() {
        initPaint();
        initPath();
    }

    private void ensurePaint() {
        if (mPaint == null) {
            mPaint = new Paint();
        }
    }

    private void ensurePath() {
        if (mPath == null) {
            mPath = new Path();
        }
    }

    private void initPaint() {
        ensurePaint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(1f);
    }

    public Bitmap getBitmap() {
        return mBitmap;
    }

    // 设置Bitmap的时候初始化shader，并设置给paint
    public void setBitmap(Bitmap bitmap, int mWidth, int mHeight) {
        this.mBitmap = bitmap;
        if (bitmap == null) {
            mShader = null;
        } else {
            mShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            mPaint.setShader(mShader);
            updateShaderMatrix(mWidth, mHeight);
        }
    }

    private int mWidth;
    private int mHeight;
    private int mLength;

    // 初始化好Path要走的路径
    private void initPath() {
        ensurePath();
        float l = (float) (mRect.width() / 2);
        float h = (float) (Math.sqrt(3) * l);
        float top = (mRect.height() - h) / 4;
        mPath.reset();
        mPath.moveTo(top / 2, l / 2 + top);//
        mPath.lineTo(h / 2 + top, top);
        mPath.lineTo(h + top * 2, l / 2 + top);
        mPath.lineTo(h + top * 2, (float) (l * 1.5) + top * 2);
        mPath.lineTo(h / 2 + top, 2 * l + top * 2);
        mPath.lineTo(top / 2, (float) (l * 1.5) + top * 2);
        mPath.lineTo(top / 2, l / 2 + top);
        mPath.close();
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawPath(mPath, mPaint);
    }

    @Override
    public void setAlpha(int alpha) {
        if (mPaint != null) {
            mPaint.setAlpha(alpha);
        }
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        if (mPaint != null) {
            mPaint.setColorFilter(colorFilter);
        }
    }

    @SuppressLint("WrongConstant")
    @Override
    public int getOpacity() {
        return 0;
    }


    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        super.setBounds(left, top, right, bottom);
        mRect.set(left, top, right, bottom);
        initPath();
    }

    @Override
    public int getIntrinsicWidth() {
        if (mBitmap != null) {
            return mBitmap.getWidth();
        } else {
            return super.getIntrinsicWidth();
        }
    }

    @Override
    public int getIntrinsicHeight() {
        if (mBitmap != null) {
            return mBitmap.getHeight();
        }
        return super.getIntrinsicHeight();
    }

    private void updateShaderMatrix(int mWidth, int mHeight) {
        int mBitmapHeight = mBitmap.getHeight();
        int mBitmapWidth = mBitmap.getWidth();
        float scalex, scaley;
        mShaderMatrix.set(null);
        if (mWidth < mBitmapWidth) {
            scalex = (float) mWidth / mBitmapWidth;
        } else {
            scalex = (float) mBitmapWidth / mWidth;
        }
        if (mHeight < mBitmapHeight) {
            scaley = (float) mHeight / mBitmapHeight;
        } else {
            scaley = (float) mBitmapHeight / mHeight;
        }
        mShaderMatrix.setScale(scalex, scaley);//放大铺满
        float dx = mWidth - mBitmapWidth * scalex;
        float dy = mHeight - mBitmapHeight * scaley;
        mShaderMatrix.postTranslate(dx / 2, dy / 2);//平移居中
        mShader.setLocalMatrix(mShaderMatrix);
    }
}